<!--头部公共部分-->
{include file="common/header" /}
<div style="padding:15px;">
    <form class="layui-form layui-form-pane">

        <div class="layui-form-item" style="text-align: center;">
            <a class="layui-btn layui-btn-lg layui-btn-radius layui-btn-danger">讲台</a>
        </div>

        <div class="layui-form-item" style="text-align: center;margin-top: 10px;">


            <script>
                var seat_row = '{$seat_row}';  //行
                var seat_column = '{$seat_column}';  //列
                var seat_nums = '{$seat_nums}'; //选中的座位数
                var arr_ = seat_nums.split(",");

                var num = 0;
                var i = 0;
                var j = 0;
                for(i = 0; i < seat_row; i ++){

                    for(j = 0; j < seat_column; j ++  ){
                        num ++;
                        if(arr_.length >=1 && arr_[0] != ""){
                            if(arr_.indexOf(num+"") != -1){   //判断数组中是否包含此项
                                document.write('<div  name="item" class="layui-inline" style="visibility: visible">');
                                document.write('<input type="checkbox" lay-skin="primary" name="ids" value="'+ num +'"  checked>');
                                document.write('</div>');

                            }else{
                                document.write('<div   name="item"  class="layui-inline" style="visibility: hidden">');
                                document.write('<input type="checkbox" lay-skin="primary" name="ids" value="'+ num +'"   disabled>');
                                document.write('</div>');
                            }

                        }else {
                            document.write('<div   name="item"  class="layui-inline" style="visibility: visible">');
                            document.write('<input type="checkbox" lay-skin="primary"  name="ids" value="'+ num +'"  >');
                            document.write('</div>');
                        }


                    }
                    document.write('<p>');

                }
            </script>

        </div>

        <div class="layui-form-item" style="text-align: center;margin-top: 10px">
            <a class="layui-btn" lay-submit lay-filter="sure">锁定</a>
            <a class="layui-btn  layui-btn-warm" lay-submit lay-filter="rest">解锁</a>
        </div>
    </form>
</div>
<!--底部公共部分-->
{include file="common/footer" /}

<script>
    var seat_id = '{$seat_id}';
    layui.use(['element', 'jquery','form','layer'], function(){
        var $ = layui.jquery
            ,element = layui.element
            ,layer = layui.layer
            ,form = layui.form;
        var inputs = document.getElementsByName("ids");
        var items =  document.getElementsByName("item");
        //监听提交
        form.on('submit(sure)', function (data) {
            var field = data.field;
            var isLoad;
            var inputs = document.getElementsByName("ids");
            var num = '';
            var status = false;
            for(var i=0;i<inputs.length;i++){

                if(inputs[i].checked){
                    status = true;
                    num = num +inputs[i].value + ',';
                }


            }

            if(!status){

                layer.msg("请选择需要锁定的座位");
                return;
            }

            if(num.length > 0){
                num = num.substr(0,num.length-1);
            }
            field.nums = num;
            field.seat_id = seat_id;
            $.ajax({
                url: '/admin/classroom/choice_seat',
                method: "post",
                data: field,
                beforeSend: function () {
                    isLoad = layer.load(0, {
                        shade: false
                    }); //0代表加载的风格，支持0-2
                },
                success: function (result) {
                    layer.msg(result.msg);
                    if (result.code === 1) {

                        setTimeout(function () {
                            for(var i=0;i<inputs.length;i++){
                                if(inputs[i].checked == false){
                                    inputs[i].disabled = true;
                                }
                            }
                            var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                            parent.layer.close(index); //再执行关闭
                            parent.location.reload();

                        }, 1000);


                    }
                },
                complete: function () {
                    layer.close(isLoad)
                },
                error: function (error) {
                    console.log(error)
                }
            });

            return false;
        });


        //监听提交
        form.on('submit(rest)', function (data) {
            var field = data.field;
            field.seat_id = seat_id;
            var isLoad;


            $.ajax({
                url: '/admin/classroom/rest_seat',
                method: "post",
                data: field,
                beforeSend: function () {
                    isLoad = layer.load(0, {
                        shade: false
                    }); //0代表加载的风格，支持0-2
                },
                success: function (result) {
                    layer.msg(result.msg);
                    if (result.code === 1) {

                        setTimeout(function () {
                            for(var i=0;i<inputs.length;i++){
                                items[i].visible = true;
                                inputs[i].checked = false;
                                inputs[i].disabled = false;
                            }
                            /*
                            var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                            parent.layer.close(index); //再执行关闭
                            parent.location.reload();
                            */
                            location.reload();
                        }, 1000);


                    }
                },
                complete: function () {
                    layer.close(isLoad)
                },
                error: function (error) {
                    console.log(error)
                }
            });
            return false;
        });
    });

</script>